*** Sum of income ***

do "E:\yungyu\lottery_baby\do\set_environment.do"

capture log close
log using "$log\1.2.incom.log" , replace
clear
set more off

forv i=93(1)109{ //Year 2004 to 2020
	use incm_pfn_data_yr incmer_idn id_tp incm_tp incm_format_cd incm_mk  payment_t_amt using "$iimh/iimh_`i'.dta" , clear
	
	keep if id_tp=="0" //keep citizen
	drop if incmer_idn=="" | incmer_idn=="9999999999" | incmer_idn=="          "
	
	rename incm_pfn_data_yr year
	rename incmer_idn idn
	rename incm_format_cd incm_cd
	rename payment_t_amt pay

	drop if incm_tp=="8" & incm_cd =="91" & incm_mk == "D" //drop lottety income
	keep year idn pay
	collapse (sum)pay , by(year idn)
	rename pay income_iimhnolottery
	compress
	
	** Income for military and teachers
	if `i' <= 100{ //previous to 2011, estimate income by insurance
		merge 1:1 year idn using "$govpay\est_gov_pay_`i'.dta"
		recode est .=0
		rename est income_est
		gen income=income_iimhnolottery+income_est
		drop _m
		lab var income_est "military and teachers estimated income"
	}
	
	if `i' > 100{
		gen income=income_iimhnolottery
	}
	
	lab var income "total income (exclude lottety income)"
	
	save "$wdata/income_`i'.dta", replace
}

log close
